﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace Notepad_X.Components
{
    /// <summary>
    /// MenuButton Component Class
    /// Author NohponeX
    /// Version 0.5
    /// </summary>
    public class MenuButton  : Button
    {
        private bool selected = false;
        private Color _highlightcolor = Color.FromArgb(255,255,255);
        private Color _bordercolor = Color.FromArgb(200, 200, 200);

        private int _selectedAlpha = 255;
        private int _overAlpha = 100;

        private Notepad_X.Classes.Icons oIcon;
        private Notepad_X.Classes.Strings oString;

        public Notepad_X.Classes.Icons Icon
        {
            get { return oIcon; }
            set { oIcon = value ; Invalidate(); }
        }

        public Notepad_X.Classes.Strings String
        {
            get { return oString; }
            set { oString = value; Invalidate(); }
        }

        [DefaultValue(255)]
        public int SelectedAlpha
        {
            get { return _selectedAlpha; }
            set { if (value < 256 && value >= 0) { _selectedAlpha = value; }; Invalidate(); }
        }

        [DefaultValue(100)]
        public int OverAlpha
        {
            get { return _overAlpha; }
            set { if (value < 256 && value >= 0) { _overAlpha = value; }; Invalidate(); }
        }

        public Color HighlightColor
        {
            get { return _highlightcolor; }
            set { _highlightcolor = value; Invalidate(); }
        }


        public Color BorderColor
        {
            get { return _bordercolor; }
            set { _bordercolor = value; Invalidate(); }
        }


        [Description("Sets if control is selected"),
         DefaultValue(false),
         Browsable(true)]
        public Boolean Selected
        {
            get { return selected; }
            set { selected = value; Invalidate(); }
        }

        private bool bMouseDown=false;
        private bool bMouseOver=false;

        public MenuButton()
        {

        }

        protected override void OnPaint(PaintEventArgs pevent)
        {

            Graphics g = pevent.Graphics;
            g.Clear( this.BackColor);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.CompositingQuality = CompositingQuality.HighQuality;

            if (bMouseDown || selected)
            {
                Rectangle outerRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
                Rectangle innerRectangle = new Rectangle(0, 0, this.Width-1 , this.Height-1);

                Pen pen1 = new Pen(Color.FromArgb(_selectedAlpha, _highlightcolor));
                Pen pen2 = new Pen(Color.FromArgb(170, _bordercolor));

                //LinearGradientBrush lgb1 = new LinearGradientBrush(outerRectangle, Color.FromArgb(100, 247, 247, 247), Color.FromArgb(100, 219, 219, 219), LinearGradientMode.Vertical);
                SolidBrush ob = new SolidBrush(Color.FromArgb(_selectedAlpha, _highlightcolor));

                g.FillRectangle(ob, outerRectangle);

                g.DrawRectangle(pen1, outerRectangle);

              
                g.DrawRectangle(pen2, innerRectangle);

                SolidBrush Brush = new SolidBrush(this.ForeColor);
                RectangleF rf = new RectangleF(0, this.Height - (this.Height / 2), this.Width, this.Height / 2);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                g.DrawString(this.Text, this.Font, Brush, rf, sf);

            }else{

                //Rectangle outerRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
                ////Rectangle innerRectangle = new Rectangle(1, 1, this.Width - 3, this.Height - 3);


                //Pen pen1 = new Pen(Color.FromArgb(100, 183, 183, 183));
                //Pen pen2 = new Pen(Color.FromArgb(100, 255, 255, 255));

                //LinearGradientBrush lgb1 = new LinearGradientBrush(outerRectangle, Color.FromArgb(100, 247, 247, 247), Color.FromArgb(100, 219, 219, 219), LinearGradientMode.Vertical);
                //SolidBrush ob = new SolidBrush(Color.FromArgb(150, 250, 250, 250));

                //g.FillRectangle(ob, outerRectangle);

                //g.DrawRectangle(pen1, outerRectangle);

                //g.DrawRectangle(pen2, innerRectangle);

                SolidBrush Brush = new SolidBrush(this.ForeColor);
                RectangleF rf = new RectangleF(0, this.Height - (this.Height / 2), this.Width, this.Height / 2);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                g.DrawString(this.Text, this.Font, Brush, rf, sf);

            }

            if (bMouseOver)
            {
                Rectangle outerRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
                //Rectangle innerRectangle = new Rectangle(1, 1, this.Width - 3, this.Height - 3);
                Rectangle innerRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

                Pen pen1 = new Pen(Color.FromArgb(100, _highlightcolor));
                //Pen pen2 = new Pen(Color.FromArgb(100, 255, 255, 255));
                Pen pen2 = new Pen(Color.FromArgb(170, _bordercolor));


                //LinearGradientBrush lgb1 = new LinearGradientBrush(outerRectangle, Color.FromArgb(100, 247, 247, 247), Color.FromArgb(100, 219, 219, 219), LinearGradientMode.Vertical);
                SolidBrush ob = new SolidBrush(Color.FromArgb(_overAlpha, _highlightcolor));

                g.FillRectangle(ob, outerRectangle);

                g.DrawRectangle(pen1, outerRectangle);

                g.DrawRectangle(pen2, innerRectangle);

                SolidBrush Brush = new SolidBrush(this.ForeColor);
                RectangleF rf = new RectangleF(0, this.Height - (this.Height / 2), this.Width, this.Height / 2);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;


                g.DrawString(this.Text, this.Font, Brush, rf, sf);

                bMouseOver = false;
            }
            if (this.Image != null)
            {
                RectangleF imgRec = new RectangleF((this.Width/2) - (this.Image.Width / 2), (this.Height / 3) - (this.Image.Height /2), this.Image.Width, this.Image.Height);
                g.DrawImage(this.Image, imgRec);
            }
            
           // base.OnPaint(pevent);
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            selected = true;
            base.OnMouseClick(e);
        }

        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            bMouseDown = true;
            base.OnMouseDown(mevent);
        }
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            bMouseDown = false;
            base.OnMouseUp(mevent);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            bMouseOver = true;
            base.OnMouseEnter(e);
        }

    }

    public class MenuButtonCollection : System.Collections.ArrayList
    {

        public MenuButtonCollection()
        {

        }
        public void AddControl(MenuButton control)
        {
            base.Add(control);
            control.Click += new EventHandler(control_Click);
        }

        void control_Click(object sender, EventArgs e)
        {
            foreach (MenuButton control in this)
            {
                if (control != sender && control.Selected)
                {
                    control.Selected = false;
                    //control.Focus();
                }
                //Console.WriteLine(control.Name);
            }
            ((MenuButton)sender).Selected = true;
            //((MenuButton)sender).Update();
        }
    }
}
